11. 模式匹配 [练习区]

模式匹配

现在,你已经知道如何迭代 2D 数组。下一项任务是:使用你所学的知识,根据某些传感器测量结果,在 2D 世界中定位机器人。

在本例中,一个机器人正在一个 5x4 的橙色和蓝色世界中行走,如下所示。这个机器人安装有一个传感器,可以告诉它目前正处于什么颜色的方格中, 以及 它正右边方格的颜色。

2D 橙色和蓝色世界方格

2D 橙色和蓝色世界方格

机器人感应两个颜色方格(一个在脚下,一个在右边)

机器人感应两个颜色方格(一个在脚下,一个在右边)

在 Python 代码中,世界和传感器测量数据可以用下面的代码表示(其中“o”代表橙色,“b”代表蓝色):

import numpy as np

world = np.array ([ ['o', 'b', 'o', 'o', 'b'],
                    ['o', 'o', 'b', 'o', 'o'],
                    ['b', 'o', 'o', 'b', 'o'],
                    ['b', 'o', 'o', 'o', 'o'] ])

measurement = ['b', 'o']

现在,根据测量结果和世界网格上的颜色模式,我们很容易判断出,这个机器人在世界上只可能位于少数几个地方!在这个练习中,你需要以编程的方式找到这些位置。

提示 :你需要把值添加到列表中。要添加索引,请使用追加函数,格式为 list_name.append([row,column])。进一步的说明可以在下面的 TODO 中找到。

Start Quiz:

import numpy as np

# A 4x5 robot world of characters 'o' and 'b'
world = np.array([ ['o', 'b', 'o', 'o', 'b'],
                   ['o', 'o', 'b', 'o', 'o'],
                   ['b', 'o', 'o', 'b', 'o'],
                   ['b', 'o', 'o', 'o', 'o'] ])

# Sensor measurement
measurement = ['b', 'o']

# This function takes in the world and the sensor measurement.
# Complete this function so that it returns the indices of the 
# likely robot locations, based on matching the measurement 
# with the color patterns in the world

def find_match(world, measurement):
    # Empty possible_locations list
    possible_locations = []
    
    ## TODO: Iterate through the world 
    ## Look at two adjacent indices at a time - the square the robot is 
    ## on top of and the square to its right
    ## (Making sure not to go past the bounds of the world)
    
    ## TODO: If two adjacent colors in the world match 
    ## the two colors in the sensor measurement
    ## Add those indices to the possible_locations list
    ## Append them in the format [row_index, column_index], i.e. [0, 0]
    
    return possible_locations
   

# This line runs the function and stores the output - do not delete 
locations = find_match(world, measurement)
import numpy as np

# A 4x5 robot world of characters 'o' and 'b'
world = np.array([ ['o', 'b', 'o', 'o', 'b'],
                   ['o', 'o', 'b', 'o', 'o'],
                   ['b', 'o', 'o', 'b', 'o'],
                   ['b', 'o', 'o', 'o', 'o'] ])

# Sensor measurement
measurement = ['b', 'o']

# This function takes in the world and the sensor measurement.
# Complete this function so that it returns the indices of the 
# likely robot locations, based on matching the measurement 
# with the color patterns in the world

def find_match(world, measurement):
    
    # Empty possible_locations list
    possible_locations = []
    
    # Store the number of columns and rows in the 2D array
    col = world.shape[1]
    row = world.shape[0]
    
    # Iterate through the entire array
    for i in range(0, row):
        for j in range (0, col):
            # Check that we are within the bounds of the world,
            # since we have to check two values, this means we're at
            # a row index < the number of columns (5) - 1
            # In other words j < 4
            if j < col - 1:
                # Check if a match is found by comparing array contents
                # and checking for equality at world[i][j] and 
                # one row to the right at world[i][j+1]
                
                # Values under and in front of the robot 
                under = world[i][j]
                in_front = world[i][j+1]
                
                if((measurement[0] == under) and (measurement[1] == in_front)):
                    # A match is found!
                    # Append the index that the robot is on
                    possible_locations.append([i,j])
    
    # Return the completed list
    return possible_locations
   

# This line runs the function and stores the output - do not delete 
locations = find_match(world, measurement)
import numpy as np

# A 4x5 robot world of characters 'o' and 'b'
world = np.array([ ['o', 'b', 'o', 'o', 'b'],
                   ['o', 'o', 'b', 'o', 'o'],
                   ['b', 'o', 'o', 'b', 'o'],
                   ['b', 'o', 'o', 'o', 'o'] ])

# Sensor measurement
measurement = ['b', 'o']

def find_match(world, measurement):
    
    # Empty possible_locations list
    possible_locations = []
    for y in range(world.shape[0]):
        for x in range(world.shape[1]):
            # if we are at the edge then we can't
            # look ahead. Use the "continue" statement
            # to proceed to the next step in the loop.
            if x == (world.shape[1]-1):
                continue
            
            m_under = world[y,x]   # get measurement UNDER robot
            m_front = world[y,x+1] # measurement in front of robot
            if [m_under, m_front] == measurement:
                possible_locations.append([y,x])
    
    return possible_locations
   

# This line runs the function and stores the output - do not delete 
locations = find_match(world, measurement)